import { specslist, specscount } from "../../../request/api"
const state = {
    specsList: [],
    //页码
    pageNum: 1,
    //一页的数据量
    pageSize: 2,
    //总条数
    count: 0
}
const getters = {
    getSpes(state) {
        return state.specsList
    },
    //总条数
    getCount(state) {
        return state.count
    },
    //每一页数据的个数
    getpageSize(state) {
        return state.pageSize
    }
}
const mutations = {
    //获取列表数据
    REQ_SPECS(state, payload) {
        state.specsList = payload
    },
    //获取总条数
    REQ_COUNT(state, payload) {
        state.count = payload
    },
    //封装一个修改页码的事件
    REQ_COUNTSPACS(state, payload) {
        state.pageNum = payload
    }
}
const actions = {
    //获取数据
    getSpecsAction(context) {
        specslist({
            page: context.state.pageNum,
            size: context.state.pageSize
        }).then(res => {
            // let list = res.list ? res.list : []
            context.commit('REQ_SPECS', res.list)
            //彻底解决  页码不是第一页  并且没有数据存在
            if (context.state.pageNum != 1 && res.list.length == 0) {
                //重新触发页码修改
                context.dispatch('getChangePage', context.state.pageNum - 1)
            }
        })
    },
    //获取总条数
    getCountAction(context) {
        specscount().then(res => {
            // console.log(res.list[0].total);
            context.commit('REQ_COUNT', res.list[0].total)
        })
    },
    //页面点击页码的时候  切换数据
    getChangePage(context, payload) {
        //修改页码的时候  修改pageNum  并重新获取列表数据
        context.commit('REQ_COUNTSPACS', payload)
        context.dispatch('getSpecsAction')
    }
}

export default {
    state, getters, mutations, actions,
    namespaced: true
}